package com.example.project;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.example.project.db.DBHelper;
import com.example.project.history.History;

public class QueryModiActivity extends Activity{
	private DBHelper mDbManager;
	private String list_no;//리스트번호=카테고리 고유 번호
	private String category;//리스트번호=카테고리 고유 번호
	private String query;//리스트번호=카테고리 고유 번호
	private Cursor cursor = null;
	private ArrayList<String> arr_list;//해당 카테고리의 쿼리목록
	private EditText cat;
	private EditText desc;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		History.ishistory = false;
		setContentView(R.layout.modiqueryactivity);
		list_no = getIntent().getExtras().getString("listno");
		category = getIntent().getExtras().getString("category");
		cat = (EditText) findViewById(R.id.modicategory);
		desc = (EditText) findViewById(R.id.modidesc);
		cat.setText(category);
		
		mDbManager = DBHelper.getInstance(this);
		
		Spinner spin1 = (Spinner) findViewById(R.id.modiquery);
		spin1.setPrompt("쿼리 선택");		
		
		cursor = mDbManager.select(list_no);
		arr_list = new ArrayList<String>();
		boolean isfirst = true;
		StringBuffer querylist = new StringBuffer();
		if (cursor.moveToFirst()) {			
			int n_query = cursor.getColumnIndex("query");
			int n_mysql = cursor.getColumnIndex("mysql");
			int n_oracle = cursor.getColumnIndex("oracle");
			int n_sqlserver = cursor.getColumnIndex("sqlserver");
			do {
				querylist.delete(0, querylist.length());
				querylist.append(cursor.getString(n_query)+"(");
				if(cursor.getInt(n_mysql)==1){
					querylist.append("mySQL");
					isfirst = false;
				}
				if(cursor.getInt(n_oracle)==1){
					if(!isfirst){
						querylist.append(", ");
					}
					querylist.append("Oracle");
					isfirst = false;
				}
				if(cursor.getInt(n_sqlserver)==1){
					if(!isfirst){
						querylist.append(", ");
					}
					querylist.append("SqlServer");
				}
				querylist.append(")");
				arr_list.add(querylist.toString());
				isfirst = true;
			} while (cursor.moveToNext());
			cursor.close();
		}
		
		final ArrayAdapter<String> adspin = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arr_list);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin1.setAdapter(adspin);
		
		spin1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				Log.d("PRINT", position+"");
				StringBuffer sb = new StringBuffer();
				query = arr_list.get(position);
				Log.d("PRINT", query);
				cursor = mDbManager.searchdesc(list_no, query.substring(0, query.indexOf('(')));
				if (cursor.moveToFirst()) {
					int n_description = cursor.getColumnIndex("description");
					do {
						String description = cursor.getString(n_description);				
						sb.append(description);
					} while (cursor.moveToNext());
					cursor.close();
				}
				desc.setText(sb);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				desc.setText("");
			}
		});
	}
	
	public void mOnClick(View v) {
		switch (v.getId()) {
		case R.id.btnmodi:			
			mDbManager.update(query.substring(0, query.indexOf('(')), desc.getText().toString());
			finish();
			/*Intent intent;
			if(History.ishistory){
				intent = new Intent(this, History.class);
			}else{
				intent = new Intent(this, MultiPaneWidth.class);
			}
			intent.addFlags(Intent.);
			startActivity(intent);*/
			break;
		}
	}
}
